package com.axend.lib_room.database

import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.axend.lib_base.utils.CommUtils
import com.axend.lib_room.dao.SofaDao
import com.axend.lib_room.entity.SofaLogEntity

/**
 * @author axend
 * @date 2020/7/30
 * @desc 数据库
 */
@Database(entities = [SofaLogEntity::class], version = 1, exportSchema = false)
abstract class SofaDataBase :RoomDatabase(){

    abstract fun sofaDao(): SofaDao


    companion object{
        private var dataBase:SofaDataBase?=null

        fun getInstance():SofaDataBase{
            return dataBase ?: Room.databaseBuilder(CommUtils.getContext(), SofaDataBase::class.java, "Sofa_DB")
            //是否允许在主线程查询，默认是false
            .allowMainThreadQueries()
                //数据库被创建或者被打开时的回调
                //.addCallback(callBack)
                //指定数据查询的线程池，不指定会有个默认的
                //.setQueryExecutor {  }
                //任何数据库有变更版本都需要升级，升级的同时需要指定migration，如果不指定则会报错
                //数据库升级 1-->2， 怎么升级，以什么规则升级
                .addMigrations(MIGRATION_1_2,MIGRATION_2_3,MIGRATION_3_4)
                //设置数据库工厂，用来链接room和SQLite，可以利用自行创建SupportSQLiteOpenHelper，来实现数据库加密
                //.openHelperFactory()
                .build()
        }
        //数据库更新1-->2
        val MIGRATION_1_2: Migration = object :Migration(1,2){
            override fun migrate(database: SupportSQLiteDatabase) {

            }
        }
        //数据库更新2-3
        val MIGRATION_2_3: Migration = object :Migration(2,3){
            override fun migrate(database: SupportSQLiteDatabase) {

            }
        }

        //数据库更新3-4
        val MIGRATION_3_4: Migration = object :Migration(3,4){
            override fun migrate(database: SupportSQLiteDatabase) {

            }
        }
    }

}